(function() {
  var rootCommentableId = <%== "comments-for-#{commentable.commentable_type.demodulize}-#{commentable.id}".to_json %>;

  var $comments = $("#" + rootCommentableId);
  var component = $comments.data("comments");

  $(".loading-comments").addClass("hidden");

  var commentsIds = [];
<% @comments.each do |comment| %>
  var commentId = <%= comment.id.to_json %>;
  commentsIds.push(commentId);
  var commentHtml = '<%== j(render comment).strip %>';
  var inReplyTo = <%== (reply?(comment) ? comment.commentable.id : nil).to_json %>;

  var $comment = $("#comment_" + commentId);
  if (1 > $comment.length) {
    if (inReplyTo) {
      component.addReply(inReplyTo, commentHtml);
    } else {
      component.addThread(commentHtml);
    }
  }
<% end %>
  if (commentsIds.length) {
    component.lastCommentId = <%= @comments.last&.id || 0 %>;

    Rails.fire(document, "comments:loaded", {
      commentsIds: commentsIds
    });
  }

  // Update the comments count
  $(".comments-count", $comments).text(<%== t("decidim.components.comments.title", count: @comments_count).to_json %>);
}());
